mini.parse();
var projectName = $("#projectName").val();
var form0 = new mini.Form("form0");
var form1 = new mini.Form("form1");
var form2 = new mini.Form("form2");
var id= mini.get("id").getValue();
var taskId= mini.get("taskId").getValue();
var dymcformId= mini.get("dymcformId").getValue();
var tableName= mini.get("tableName").getValue();
autoGridLoad();
function isExitsFunction(functionName){
	var result = true;
	try{
		if(eval(functionName+"()")){ //functionName 方法执行结果为true
			result = true;
		}else{ //functionName 方法执行结果为false;
			result = false;	
		}
	}catch(e){ // 没有定义functionName方法
		console.log(e);
		result = true;
	}
	return result;
}
/**保存*/
function onClick(e) {
	if(isExitsFunction("beforeSave")){
		 SaveFormData();
	}
 }
/**发送**/
function onSend(e){
	// 先验证
	if(isExitsFunction("beforeSend")){
		SaveFormData(callFlowInfc);
	}
}

function SaveFormData(callback) {
	if(callback && typeof(callback)=="function"){
		form2.validate();
		if (form2.isValid() == false) return;
	}
	var json1 = mini.encode(getSubtableData(GetFormData(form2)));
	mini.get("htmlValue").setValue(json1);
	var json = mini.encode(GetFormData(form1));
	mini.mask({
        el: document.body,
        cls: 'mini-mask-loading',
        html: '正在保存信息...'
    }); 
	$.ajax({
		url : projectName+"/DymcformDisplayAdmin/saveFormData",
		type : 'post',
		contentType : "application/json; charset=utf-8",
		dataType : 'json',
		data : json,
		cache : false,
		success : function(data) {
			mini.unmask();
			if (data.status == '1') {
				id = data.result.id;
				if(data.result.version){
					mini.get("version").setValue(data.result.version);
				}
				mini.get("id").setValue(data.result.id);
				mini.get("pkvalue").setValue(data.result.id);
				if(callback && (typeof(callback) == "function")){
					callback();
				}else{
					mini.alert(data.message, "" , function() {
						
					});
				}
			} else if(data.status == '0') {
				mini.alert(data.message);
			} else {
				mini.alert("发生错误!");
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
		    mini.unmask();
			mini.alert(jqXHR.responseText);
		}
	});
}

function GetFormData(form_Name){
	var o = form_Name.getData();
	return o;
}
function CloseWindow(action) {
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
/**获取子表的数据**/
function getSubtableData(o){
	var subtableList = [];//
	$.each($(".subtable"),function(){
		var name = $(this).attr("name");
		var grid = mini.get(name+"_datagrid");
		try{
			var obj = grid.getChanges();
			o[name] = obj;
			console.log("获取数据成功"+eval('obj'));
		}catch(e){
			console.log("获取子表数据失败"+name);
		}
	});
	if(mini.get("dymcformId").getValue().toUpperCase() == "K_DYMCFORM_20170524142530"){
		if(mini.get("taskKey").getValue() == "start" ||mini.get("taskKey").getValue()=="usertask1" ){
			var dd = mini.clone(o["equipment_list"]);
			o["equipment_detail"] = dd;
		}
	}
	if(mini.get("dymcformId").getValue().toUpperCase() == "K_DYMCFORM_20170626094847"){
		if(mini.get("taskKey").getValue() == "start" ||mini.get("taskKey").getValue()=="usertask1" ){
			var dd = mini.clone(o["old_equipment_list"]);
			o["new_equipment_list"] = dd;
		}
	}
	return o;
}
/**子表初始化加载*/
function autoGridLoad(){
	$.each($(".subtable"),function(){
		var name = $(this).attr("name");
		var grid = mini.get(name+"_datagrid");
		try{
			eval("grid.load()");
		}catch(e){
			console.log(e);
		}
		
	});
}
/**调用流程变量**/
function callFlowInfc(){
	var json = mini.encode(GetFormData(form0));
	mini.mask({
        el: document.body,
        cls: 'mini-mask-loading',
        html: '正在发送信息...'
    }); 
	$.ajax({
		url : projectName+'/ActivitiTaskAdmin/startWork',
		type : 'post',
		contentType : "application/json; charset=utf-8",
		dataType : 'json',
		data : json,
		cache : false,
		success : function(data) {
			mini.unmask();
			if (data.status == '1') {
				mini.alert(data.message, "" , function() {
					CloseWindow("");
				});
			} else if(data.status == '0') {
				mini.alert(data.message);
			} else {
				mini.alert("发生错误!");
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
		    mini.unmask();
			mini.alert(jqXHR.responseText);
		}
	});
}
